package com.pw.study.file

import java.io.FileOutputStream
import java.nio.charset.StandardCharsets

import scala.io.{BufferedSource, Source}

/**
 * @Desc: 读文件，写文件
 * @author: future
 * @since: 2022/5/22-14:13
 */
object ReadFile {
  /**
   * 文件读取
   * @param fileName
   * @return
   */
  def readFile(fileName: String): List[String] = {
    val source: BufferedSource = Source.fromFile(fileName)
    val lines: Iterator[String] = source.getLines()
    val list: List[String] = lines.toList
    list
  }

  /**
   * 文件写入
   * @param list
   */
  def writeFile(list: List[String]) = {
    val curDir = "data/file/"
    val outFile = new FileOutputStream(curDir + "school.txt")
    list.foreach(line => {
      val lineByte: Array[Byte] = line.getBytes(StandardCharsets.UTF_8)
      outFile.write(lineByte)
      outFile.write(("line:" + line + "\r\n").getBytes())
    })
    outFile.close()
  }

  def main(args: Array[String]): Unit = {
    val fileName = "data/file/sc.txt"
    val list: List[String] = readFile(fileName)
    writeFile(list)

  }
}
